
(function($)
{
	var coolforms = 
	{
		/* used to assigned generated ids */
		_currentId: 1,
		
		/* generate id for objects who don't have one */
		_getId: function(obj)
		{
			if(obj.id)
				return obj.id;
			return ('__coolForms_' + (this._currentId++));
		},
		
		create: function(form)
		{
			$('#' + form.id).addClass("cf");
			this.makeIntro(form.id, form.title, form.description);
			if(form.sections)
				for(var i = 0; i < form.sections.length; i++)
					this.makeSection(form.id, form.sections[i]);
		},
		
		makeIntro: function(parentId, title, description)
		{
			$('#' + parentId).append('<div class="cfIntro" id="cfItntro_' + parentId + '">');
			if(title)
				$('#' + parentId).append('<h1>' + title + '</h1>');
			if(description)
				$('#' + parentId).append(description);
			$('#' + parentId).append('</div>');
		},
		
		makeSection: function(parentId, section)
		{
			sectionId = this._getId(section);
			$('#' + parentId).append('<div class="cfSection" id="' + sectionId + '"></div>');
			if(section.title)
				$('#' + sectionId).append('<h2>' + section.title + '</h2>');
			if(section.description)
				$('#' + sectionId).append(section.description);	
			if(section.groups)
				for(var i = 0; i < section.groups.length; i++)
					this.makeGroup(sectionId, section.groups[i]);
		},
		
		makeGroup: function(parentId, group)
		{
			groupId = this._getId(group);
			$('#' + parentId).append('<div class="cfGroup" id="' + groupId + '"></div>');
			if(group.title)
				$('#' + groupId).append('<h3>' + group.title + '</h3>');
			if(group.description)
				$('#' + groupId).append(group.description);
			if(group.lines)
				for(var i = 0; i < group.lines.length; i++)
					this.makeLine(groupId, group.lines[i]);
		},
		
		makeLine: function(parentId, line)
		{
			lineId = this._getId(line);
			$('#' + parentId).append('<div class="cfLine" id="' + lineId + '"></div>');
			if(line.inputs)
				for(var i =0; i < line.inputs.length; i++)
					this.makeInput(lineId, line.inputs[i]);
		},
		
		makeInput: function(parentId, input)
		{
			
			inputId = this._getId(input);
			var size = 1; // default size
			if(input.size)
				size = input.size;
			$('#' + parentId).append('<div class="cfField' + size + '" id="' + inputId + '"></div>');
			if(input.title)
			{
				var title = input.title;
				if(input.mandatory && input.mandatory == true)
					title += '<span class="cfFieldMandatory">*</span>';
				$('#' + inputId).append('<h4>' + title + '</h4>');
			}
				
			
			switch(input.type)
			{
				case 'text': this._makeInputText(inputId, input); break;
				case 'radio': this._makeRadio(inputId, input); break;
			}
			
			if(input.help)
				$('#' + inputId).append('<span class="cfFieldHelp" id="' + inputId + '_help">' 
					+ input.help + '</span>');
		},
		
		_makeInputText: function(parentId, input)
		{
			$('#' + parentId).append('<input type="text" name="' + input.name + '" id="' + parentId + '_field" />');
		},
		
		_makeRadio: function(parentId, input)
		{
			for(i = 0; i < input.choices.length; i++)
			{
				if(input.choices[i].checked && input.choices[i].checked == true)
					$('#' + parentId).append('<input type="radio" name="' + input.choices[i].name 
						+ '" checked="checked" />');
				else
					$('#' + parentId).append('<input type="radio" name="' + input.choices[i].name 
						+ '">' + input.choices[i].label + '</input>');
				
			}
		}
	};
	
	$.coolforms = coolforms;

})(jQuery);